package com.hzjy.manage.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.hzjy.api.beans.HzjyCollection;
import com.hzjy.api.beans.HzjyProduct;
import com.hzjy.api.beans.HzjyProject;
import com.hzjy.api.beans.HzjyWorks;
import com.hzjy.api.mapper.ApiProductMapper;
import com.hzjy.api.mapper.ApiProjectMapper;
import com.hzjy.api.mapper.ApiWorksMapper;
import com.hzjy.api.service.impl.ApiDelistServiceImpl;
import com.hzjy.api.service.impl.ApiProductServiceImpl;
import com.hzjy.common.enums.TradingStatusEnum;
import com.hzjy.common.utils.JSONUtil;
import com.hzjy.common.utils.PageData;
import com.hzjy.common.utils.StringUtils;
import com.hzjy.manage.mapper.HzjyDelistMapper;
import com.hzjy.manage.mapper.HzjyProductMapper;
import com.hzjy.manage.service.HzjyDelistService;
import com.hzjy.manage.vo.HzjyDelist;
import com.hzjy.manage.vo.HzjyDelistDTO;
import com.hzjy.manage.vo.HzjyDelistVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

/**
 * 摘牌申请Service业务层处理
 *
 * @author hzjy
 * @date 2020-08-07
 */
@Service
public class HzjyDelistServiceImpl implements HzjyDelistService
{
    @Autowired
    private HzjyDelistMapper hzjyDelistMapper;
    @Autowired
    private ApiProductMapper apiProductMapper;
    @Autowired
    private ApiProjectMapper apiProjectMapper;
    @Autowired
    private ApiDelistServiceImpl apiDelistServiceImpl;
    @Autowired
    private ApiWorksMapper apiWorksMapper;

    /**
     * 查询摘牌申请
     *
     * @param delistId 摘牌申请ID
     * @return 摘牌申请
     */
    @Override
    public HzjyDelistVO selectHzjyDelistById(Long delistId)
    {
        return hzjyDelistMapper.selectHzjyDelistById(delistId);
    }

    /**
     * 查询摘牌申请列表
     *
     * @param pageData 摘牌申请
     * @return 摘牌申请
     */
    @Override
    public List<HzjyDelistVO> selectHzjyDelistList(HzjyDelistVO HzjyDelist)
    {
        return hzjyDelistMapper.selectHzjyDelistList(HzjyDelist);
    }

    /**
     * 新增摘牌申请
     *
     * @param hzjyDelist 摘牌申请
     * @return 结果
     */
    @Override
    public int insertHzjyDelist(HzjyDelist hzjyDelist)
    {
        return hzjyDelistMapper.insertHzjyDelist(hzjyDelist);
    }

    /**
     * 修改摘牌申请
     *
     * @param hzjyDelist 摘牌申请
     * @return 结果
     */
    @Override
    public int updateHzjyDelist(HzjyDelist hzjyDelist)
    {
        return hzjyDelistMapper.updateHzjyDelist(hzjyDelist);
    }

    /**
     * 批量删除摘牌申请
     *
     * @param delistIds 需要删除的摘牌申请ID
     * @return 结果
     */
    @Override
    public int deleteHzjyDelistByIds(Long[] delistIds)
    {
        return hzjyDelistMapper.deleteHzjyDelistByIds(delistIds);
    }

    /**
     * 删除摘牌申请信息
     *
     * @param delistId 摘牌申请ID
     * @return 结果
     */
    @Override
    public int deleteHzjyDelistById(Long delistId)
    {
        return hzjyDelistMapper.deleteHzjyDelistById(delistId);
    }

    @Override
    public HzjyProduct queryProductInfo(HzjyProduct hzjyProduct) {
        HzjyProduct newHzjyProduct = apiProductMapper.selectHzjyProductById(hzjyProduct.getProductId());

        if (newHzjyProduct.getTradingType() == 1) {
            //项目
            HzjyProject queryProject = new HzjyProject();
            queryProject.setProductId(newHzjyProduct.getProductId().toString());
            List<HzjyProject> hzjyProjectList = apiProjectMapper.selectHzjyProjectList(queryProject);
            HzjyProject hzjyProject = hzjyProjectList.get(0);
            newHzjyProduct.setHzjyProject(hzjyProject);

            //查询作品可出售权利
            if (newHzjyProduct.getState() == TradingStatusEnum.TradingSuccess.getCode()) {
                newHzjyProduct.setSalability(hzjyProject.getTradingPower());
            } else if (newHzjyProduct.getState() == TradingStatusEnum.PartialCompleted.getCode()) {
                if (newHzjyProduct.getTermType() == 1) {
                    if (hzjyProject.getPermissionType() == 0) {
                        //专有许可 只能摘牌一次
                        String[] salePower = hzjyProject.getTradingPower().split(",");
                        List<String> salePowerList = Arrays.asList(salePower);
                        List<String> pList = new ArrayList<String>(salePowerList);
                        com.hzjy.api.beans.HzjyDelist queryDelist = new com.hzjy.api.beans.HzjyDelist();
                        queryDelist.setProductId(newHzjyProduct.getProductId());
                        //已经摘牌申请的产品
                        List<com.hzjy.api.beans.HzjyDelist> haveDelist = apiDelistServiceImpl.selectHzjyDelistList(queryDelist);
                        if (!CollectionUtils.isEmpty(haveDelist)) {
                            //已经有人摘牌当前许可
                            //判断当前摘牌的权利跟
                            for (com.hzjy.api.beans.HzjyDelist delist : haveDelist) {
                                String[] delistPower = delist.getDelistPower().split(",");
                                for (String power : delistPower) {
                                    if (salePowerList.contains(power)) {
                                        pList.remove(power);
                                    }
                                }
                            }
                        }
                        if (CollectionUtils.isEmpty(pList)) {
                            newHzjyProduct.setSalability(null);
                        } else {
                            //还有可售出权利
                            String saleP = "";
                            for (String power : pList) {
                                saleP += power + ",";
                            }
                            saleP = saleP.substring(0, saleP.length() - 1);
                            newHzjyProduct.setSalability(saleP);
                        }
                    } else if (hzjyProject.getPermissionType() == 1) {
                        //非专有许可
                        newHzjyProduct.setSalability(hzjyProject.getTradingPower());
                    }
                } else {
                    //转让
                    newHzjyProduct.setSalability(hzjyProject.getTradingPower());
                }
            } else if (newHzjyProduct.getState() == TradingStatusEnum.PartialCompleted.getCode()) {
                newHzjyProduct.setSalability(null);
            }
        } else if (newHzjyProduct.getTradingType() == 0) {
            //作品
            HzjyWorks queryWorks = new HzjyWorks();
            queryWorks.setProductId(hzjyProduct.getProductId());
            List<HzjyWorks> hzjyWorksList = apiWorksMapper.selectHzjyWorksList(queryWorks);
            HzjyWorks hzjyWorks = hzjyWorksList.get(0);
            newHzjyProduct.setHzjyWorks(hzjyWorks);
            newHzjyProduct.setSalability(hzjyWorks.getTradingPower());
        }
        return newHzjyProduct;
    }

    /**
     * 摘牌清单列表
     * @return
     */
    public List<HzjyDelistDTO> selectAuditDelistList() {
        List<HzjyDelistDTO> hzjyDelistDTOList = hzjyDelistMapper.selectAuditDelistList();
        for (HzjyDelistDTO hzjyDelistDTO : hzjyDelistDTOList) {
            String recordImage = hzjyDelistDTO.getRecordImage();
            if (StringUtils.isNotBlank(recordImage)){
                //当备案不为空时，并且上传的是pdf，返回一个pdf默认回显图片
                String substring = recordImage.substring(recordImage.lastIndexOf("."));
                if (substring.equals(".pdf")){
                    hzjyDelistDTO.setPdfUrl(hzjyDelistDTO.getRecordImage());
                    hzjyDelistDTO.setRecordImage("http://test.banquango.com:8080/profile/pdf.jpg");
                }
            }
        }
        return hzjyDelistDTOList;
    }
}